/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.project.web;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.foreveross.webbase.common.config.Global;
import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.web.BaseController;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.project.entity.SysSpProject;
import com.foreveross.webbase.project.entity.SysSpServle;
import com.foreveross.webbase.project.entity.SysSpSoftware;
import com.foreveross.webbase.project.service.SysSpProjectService;
import com.foreveross.webbase.project.service.SysSpServleService;
import com.foreveross.webbase.project.service.SysSpSoftwareService;

/**
 * 项目管理Controller
 * @author yangwanli
 * @version 2017-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/project/sysSpProject")
public class SysSpProjectController extends BaseController {

	@Autowired
	private SysSpProjectService sysSpProjectService;
	@Autowired
	private SysSpSoftwareService sysSpSoftwareService;
	@RequiresPermissions("project:sysSpProject:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysSpProject sysSpProject) {
		Page<SysSpProject> page = sysSpProjectService.findPage(new Page<SysSpProject>(request(), response()), sysSpProject); 
		attr("page", page);
		return "project/sysSpProjectList";
	}

	@RequiresPermissions("project:sysSpProject:view")
	@RequestMapping("form")
	public String form(String id,SysSpSoftware sysSpSoftware) {
		System.out.println();
		if(StringUtils.isNotEmpty(id)) {
			SysSpProject sysSpProject=sysSpProjectService.get(id);
			attr("sysSpProject", sysSpProject);
		} else {
			attr("sysSpProject",new SysSpProject());
		}
		attr("obj",sysSpSoftwareService.findList(sysSpSoftware));
		return "project/sysSpProjectForm";
	}

	@RequiresPermissions("project:sysSpProject:edit")
	@RequestMapping("save")
	public String save(SysSpProject sysSpProject, RedirectAttributes redirectAttributes) {
		if (!beanValidator(redirectAttributes, sysSpProject)){
			return form(sysSpProject.getId(), null);
		}
		sysSpProjectService.save(sysSpProject);
		addMessage(redirectAttributes, "保存项目成功");
		return "redirect:"+Global.getAdminPath()+"/project/sysSpProject/?repage";
	}
	
	@RequiresPermissions("project:sysSpProject:edit")
	@RequestMapping("delete")
	public String delete(@RequestParam String id, RedirectAttributes redirectAttributes) {
		sysSpProjectService.delete(id);
		addMessage(redirectAttributes, "删除项目成功");
		return "redirect:"+Global.getAdminPath()+"/project/sysSpProject/?repage";
	}

}